import * as cc from 'cc';
import { InputUtil } from '../../component/input/InputUtil';
import { FixedUpdate } from '../../component/shadule/FixedUpdate';
import { KeyInput } from '../2d-platform/script/core/KeyInput';
import { FixedKeyInput } from '../2d-platform/script/core/FixedKeyInput';
const { ccclass, property } = cc._decorator;

@ccclass('MoveTest')
export class MoveTest extends cc.Component {
    @property(cc.Node)
    currentNode: cc.Node = null!;

    @property(cc.Label)
    label: cc.Label = null!;

    @property
    moveSpeed = 10;

    isMove = false;
    lastX = 0;
    realX = 0;

    t = 0;
    onLoad() {
        cc.input.on(cc.Input.EventType.KEY_DOWN, this.down, this);
        cc.input.on(cc.Input.EventType.KEY_UP, this.up, this);

        FixedUpdate.instance.node.on(
            FixedUpdate.EventFixedUpdate,
            this.fixedUpdate,
            this,
        );
        this.lastX = this.node.x;
        this.realX = this.node.x;

        KeyInput;
    }

    down(event: cc.EventKeyboard) {
        this.isMove = true;
    }

    up(event: cc.EventKeyboard) {
        this.isMove = false;
    }

    start() {}

    update(deltaTime: number) {
        this.t += deltaTime * FixedUpdate.instance.fixedFPS;
        this.t = this.t > 1 ? 1 : this.t; //如果卡顿很久
        this.node.x = cc.lerp(this.lastX, this.realX, this.t);
        // this.node.x = this.x;
    }

    fixedUpdate(count: number) {
        // console.log('fixedUpdate:', cc.game.deltaTime);
        this.t = 0;
        this.lastX = this.realX;
        if (FixedKeyInput.instance.getKey(cc.KeyCode.ARROW_RIGHT)) {
            this.realX += this.moveSpeed;
            this.currentNode.x = this.realX;
        }
        if (this.label.string) {
            this.label.string = FixedUpdate.instance.frameIndex.toString();
        }
    }
}
